package com.kx.center.service.impl;

import com.kx.center.common.CustomHashMap;
import com.kx.center.entity.Account;
import com.kx.center.entity.StoreInfo;
import com.kx.center.mappers.StoreInfoMapper;
import com.kx.center.service.StoreManagerService;
import com.kx.center.util.SQLUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by acmen on 16-1-5.
 */
@Service
public class StoreManagerServiceImpl implements StoreManagerService{

    @Autowired
    private StoreInfoMapper storeInfoMapper;

    @Override
    public int addStoreInfo(StoreInfo storeInfo) {
        return storeInfoMapper.insertStoreInfo(storeInfo);
    }

    @Override
    public StoreInfo findStoreInfoById(int storeInfoId) {
        return storeInfoMapper.findStoreInfoById(storeInfoId);
    }

    @Override
    public StoreInfo findStoreInfoByStoreName(int companyId, String storeName) {
        return storeInfoMapper.findStoreInfoByStoreName(companyId,storeName);
    }

    @Override
    public List<StoreInfo> getStoreInfoList(int pageNum, int pageSize, Map<String, Object> whereMap) {
        String order = "id desc";
        String limitStr = pageNum * pageSize +","+(pageSize+1);

        String where = "";
        where = SQLUtil.builWhere(whereMap);
        List<StoreInfo> storeInfoList = storeInfoMapper.findStoreInfoList(where, order, limitStr);

        if(storeInfoList == null){
            storeInfoList = new ArrayList<StoreInfo>();
        }

        return storeInfoList;
    }

	@Override
	@SuppressWarnings("unchecked")
	public void updateStoreInfoById(Account userAccount, HashMap<String, Object> map) {
		
		CustomHashMap<String, Object> item = CustomHashMap.convert(map);
		StoreInfo storeInfo = new StoreInfo();
		storeInfo.setId(item.getInt("id"));
		storeInfo.setFreightFee(item.getDouble("freight_fee"));
		storeInfo.setShippingDate(item.getString("shipping_date"));
		storeInfo.setShippingStartTime(item.getTime("shipping_start_time"));
		storeInfo.setShippingEndTime(item.getTime("shipping_end_time"));
		storeInfo.setShopOwner(item.getString("shop_owner"));
		storeInfo.setStoreDesc(item.getString("store_desc"));
		storeInfo.setFreightType(item.getInt("freight_type"));
		storeInfo.setStoreName(item.getString("store_name"));
		storeInfo.setPaymentType(item.getInt("payment_type"));
		storeInfo.setPushType(item.getInt("push_type"));
		storeInfo.setMobile(item.getString("mobile"));
		storeInfo.setOperatorId(userAccount.getOperatorId());
		storeInfoMapper.updateStoreInfoById(storeInfo);
	}

	@Override
	public void deleteStoreById(int id) {
		storeInfoMapper.deleteStoreById(id);		
	}
}
